<?php

class GroupController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	
	public function init() {
		
		$this->commonAction();	
	
	}
	//用户首页
	public function indexAction() {
		try {
			$groupname = $this->_request->getParam ( 'groupname' );
			$where='';
			if ( $groupname){
				$where.=' and groupname like "%'.$groupname.'%" or groupname_en like "%'.$groupname.'%" ';
			}
			$sql="SELECT groupid,groupname FROM `groups` where 1 ".$where;
			$result=$this->_dbAdapter ->fetchAssoc ( $sql );
			
			$sql="SELECT groupid,count(*) FROM `school` where 1 group by groupid";
			$school=$this->_dbAdapter ->fetchPairs ( $sql );
			
			$sql="SELECT groupid,count(*) FROM service_school_order a left join `school` b on a.schoolid=b.schoolid where a.isdelete=0 and start_time<'".(strtotime(date('Y-m-d'))+86400)."' and end_time>'".(strtotime(date('Y-m-d'))-86400)."' and productid=1 group by groupid";
			$service=$this->_dbAdapter ->fetchPairs ( $sql );
			//print_r($service);exit;
			
			$this->view->result=$result;
			$this->view->groupname=$groupname;
			$this->view->school=$school;
			$this->view->service=$service;
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
/*	
	//用户首页
	public function opaddAction() {
		try {
			if(! $this->isPost ()){
				throw new Exception ( '参数错误！' );
			}
			$p=$this->_request->getPost();
			
			if (empty ( $p['groupname'])) {
				throw new Exception ( '用户名不能为空！' );
			}
			if (empty ( $p['groupname_en'])) {
				throw new Exception ( '英文用户名不能为空！' );
			}
			if (empty ( $p['password'] )) {
				throw new Exception ( '密码不能为空！' );
			}
			if ($p['repassword'] != $p['repassword']) {
				throw new Exception ( '两次密码不一致！' );
			}
			if (empty ( $p['email'] )) {
				throw new Exception ( '邮箱不能为空！' );
			}
			if (empty ( $p['mobile'] )) {
				throw new Exception ( '联系方式不能为空！' );
			}
			
			$p['password']=$this->authcode($p['password'],'','mactop');
			$sql="SELECT * FROM `groups` where groupname='".$p['groupname']."'";
			$result=$this->_dbAdapter->fetchAll($sql);
			if($result){
				throw new Exception ( '用记名已存在！' );
			}else{
				$lasttime=time();
				$data=array(
					'groupname'=>$p['groupname'],
					'groupname_en'=>$p['groupname_en'],
					'password'=>$p['password'],
					'lasttime'=>$lasttime,
					'email'=>$p['email'],
					'mobile'=>$p['mobile'],
				);
				//print_r($data);exit;
				$res=$this->_dbAdapter->Insert('group',$data);
				
				if( $res )
					$this->feedback ( '增加成功', '注意', '/'.$this->controller, 'warning' );
				else 
					throw new Exception ( '增加失败 ！' );	
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
*/

	//增加集团页
	public function addgroupAction() {
		try {
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function opaddAction() {
		try {
			 if(! $this->isPost ()){
					throw new Exception ( '参数错误！' );
				}
			 $p=$this->_request->getPost();
		     //print_r($p);  exit;
			 $p['password']='111111';
			 $group=array(
			 	'groupname'=>$p['groupname'],
			 	'groupname_en'=>$p['groupname_en'],
			 	//'password'=>md5(trim($p['password'])),
			 	'is_article_self'=>1,
				'addtime'=>time(),
			 	'dbname'=>$p['groupname_en']
			 );
			 $res1=$this->_dbAdapter ->Insert('groups',$group);
		     $groupid=$this->_dbAdapter->lastInsertId(); 
			 
			 $groupadmin=array(
			 	'schoolid'=>0,
			 	'adminname'=>$p['groupadmin'],
			 	'password'=>md5(trim($p['password'])),
			 	'createtime'=>time(),
				'groupid'=>$groupid,
				'password_code'=>$p['password']
			 );
			 $res2=$this->_dbAdapter ->Insert('admin',$groupadmin);
			 
			 
			 
			    if($res1 && $res2 ){
				   $this->feedback ( '成功', '注意', '/'.$this->controller.'/index', 'tip',0); 		 
				}else{
				   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
				}  
			
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}



	//用户首页
	public function editAction() {
		try {
			//获取用户id
			$id = ( int ) $this->_request->getParam ( 'id' );
			if (! $id){
				throw new Exception ( 'parameter_error' );
			}else{
				
				$sql="SELECT * FROM `groups` where groupid=".$id;
				$result=$this->_dbAdapter ->fetchRow ( $sql );
				$this->view->result=$result;
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

  
      public function  opAction() {
		try {
	     if(! $this->isPost ()){
				throw new Exception ( '参数错误！' );
			}
		$p=$this->_request->getPost();
	   // print_r($p);  exit;
		
		$result=$this->_dbAdapter ->Update('groups',array('is_article_self'=>$p['str']),'groupid='.$p['id'] ); 
		if($result){ 
			  echo 1; exit;
			}else{
			   echo -1; exit; 
				}
	 
	  
		} catch ( Exception $e ) {
					$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
				}
			}

	public function cooperationAction() {
		try {

			$groupid = ( int ) $this->_request->getParam ( 'groupid' );
			$keyword = $this->_request->getParam ( 'keyword' );
			//print_r($groupid);print_r($keyword);exit;
			$where='';
			if ( $keyword){
				$where.=' and schoolname like "%'.$keyword.'%" ';
				$where1.=' and b.schoolname like "%'.$keyword.'%" ';
			}
			$where_group='';
			if ( $groupid){
				$where_group.=' and groupid ='.$groupid;
				$where_group1.=' and b.groupid ='.$groupid;
			}
			$this->view->groupid=$groupid;
			$this->view->keyword=$keyword;
			
			$sql="SELECT groupid,groupname FROM `groups` where 1 ";
			$group=$this->_dbAdapter ->fetchPairs ( $sql );
			$this->view->group=$group;
			
            $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    		$offset = ($curpage - 1) * $count; 
			$sql="SELECT SQL_CALC_FOUND_ROWS b.schoolid,b.schoolname,b.city,b.groupid,a.start_time,a.end_time,a.isopen_chunyu,a.school_orderid from service_school_order a LEFT JOIN school b on a.schoolid=b.schoolid where a.isdelete=0 and a.start_time<'".(strtotime(date('Y-m-d'))+86400)."' and a.end_time>'".(strtotime(date('Y-m-d'))-86400)."' and a.productid=1 ".$where_group1.$where1 ." limit $offset,$count";
			//echo $sql;exit;
			$school=$this->_dbAdapter ->fetchAssoc ( $sql );
    		$param ['file'] = '/' . $this->controller . '/' . $this->action;
    		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    		$param ['perpagenum'] = $count; // 每页显示的数目
    		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    		$param ['curpage'] = $curpage; // 当前页码
			$this->view->allnum=$param ['totalnum'];

    		$page = new MyClass_Page ( $param );
    		$page->setvar ( array ('groupid' => $groupid,'keyword'=>$keyword) );
			$this->view->page= $page->getNumPage (); 
			$this->view->school=$school;
			
			if($school){
			$schoolid=implode(',',array_keys($school));
			$sql="select schoolid,classid from class where isdel=0 and schoolid in (".$schoolid.") group by schoolid";
			$classnum=$this->_dbAdapter->fetchAssoc($sql);
			$this->view->classnum=$classnum;
			//print_r($classnum);
			
			$teacher=array();
			if($classnum){
				foreach($classnum as $key=>$value){
					$sql="select count(*) from teacher_class where inactive=0 and classid in (".implode(',',$value).") ";
					$teachernum=$this->_dbAdapter->fetchOne($sql);
					$teacher[$key]=$teachernum;				
				}
				$this->view->teacher=$teacher;
				//print_r($teacher);
			}
			$sql="select schoolid,count(distinct studentid) from group_student_class_parents_v where inactive=0 and schoolid in (".$schoolid.") group by schoolid";
			$student=$this->_dbAdapter->fetchPairs($sql);
			$this->view->student=$student;
			//print_r($student);exit;
			}
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	public function overtimeAction() {
		try {

			$groupid = ( int ) $this->_request->getParam ( 'groupid' );
			$keyword = $this->_request->getParam ( 'keyword' );
			//print_r($groupid);print_r($keyword);exit;
			$where='';
			if ( $keyword){
				$where.=' and schoolname like "%'.$keyword.'%" ';
			}
			$where_group='';
			if ( $groupid){
				$where_group.=' and groupid ='.$groupid;
			}
			$this->view->groupid=$groupid;
			$this->view->keyword=$keyword;
			
			$sql="SELECT groupid,groupname FROM `groups` where 1 ";
			$group=$this->_dbAdapter ->fetchPairs ( $sql );
			$this->view->group=$group;
			
			$sql="SELECT schoolid FROM service_school_order  where isdelete=0 and start_time<'".(strtotime(date('Y-m-d'))+86400)."' and end_time>'".(strtotime(date('Y-m-d'))-86400)."' and productid=1 ";
			$schoolid=$this->_dbAdapter ->fetchCol ( $sql );
			//print_r($schoolid);exit;

			$sql="SELECT b.schoolid,b.schoolname,b.city,b.groupid,a.start_time,a.end_time,isopen_chunyu,a.schoolid,a.school_orderid from service_school_order a LEFT JOIN school b on a.schoolid=b.schoolid where a.end_time<'".(strtotime(date('Y-m-d'))-86400)."' and a.productid=1 ".$where_group.$where ." ";
			//echo $sql;exit;
			$school_overtime=$this->_dbAdapter ->fetchAssoc ( $sql );
			$this->view->school_overtime=$school_overtime;


			$where_schoolid='';
			if($schoolid){
				$where_schoolid=' and schoolid not in ('.implode(',',$schoolid).') ';
				if($school_overtime){
					$where_schoolid .=' or schoolid in ('.implode(',',array_keys($school_overtime)).')';
				}
			}
			
            $count = 20;
			$curpage = ( int ) $this->_request->getParam ( 'page' );
	     	if (! $curpage)
    				$curpage = 1;
    		$offset = ($curpage - 1) * $count; 
			$sql="SELECT SQL_CALC_FOUND_ROWS schoolid,schoolname,city,groupid from school where 1 ".$where_group.$where .$where_schoolid." limit $offset,$count";
			//echo $sql;exit;
			$school=$this->_dbAdapter ->fetchAssoc ( $sql );
    		$param ['file'] = '/' . $this->controller . '/' . $this->action;
    		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'SELECT FOUND_ROWS()' ); 
    		$param ['perpagenum'] = $count; // 每页显示的数目
    		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
    		$param ['curpage'] = $curpage; // 当前页码
			$this->view->allnum=$param ['totalnum'];

    		$page = new MyClass_Page ( $param );
    		$page->setvar ( array ('groupid' => $groupid,'keyword'=>$keyword) );
			$this->view->page= $page->getNumPage (); 
			$this->view->school=$school;
			//print_r($school);exit;
			
			
			if($school){
				$schoolid=implode(',',array_keys($school));
				$sql="select schoolid,classid from class where isdel=0 and schoolid in (".$schoolid.") group by schoolid";
				$classnum=$this->_dbAdapter->fetchAssoc($sql);
				$this->view->classnum=$classnum;
				//print_r($classnum);
				
				$teacher=array();
				if($classnum){
					foreach($classnum as $key=>$value){
						$sql="select count(*) from teacher_class where inactive=0 and classid in (".implode(',',$value).") ";
						$teachernum=$this->_dbAdapter->fetchOne($sql);
						$teacher[$key]=$teachernum;				
					}
					$this->view->teacher=$teacher;
					//print_r($teacher);
				}
				
				$sql="select schoolid,count(distinct studentid) from group_student_class_parents_v where inactive=0 and schoolid in (".$schoolid.") group by schoolid";
				$student=$this->_dbAdapter->fetchPairs($sql);
				$this->view->student=$student;
			}//print_r($schoolid);exit;
			
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}

	 public function detailedAction() {
		try {  	
		    $groupid = ( int ) $this->_request->getParam ( 'gid' );
		    $schoolid = $this->_request->getParam ( 'sid' ); 
		    $orderid = $this->_request->getParam ( 'orderid' ); 
			//print_r($groupid);print_r($schoolid);exit;
			$this->view->groupid=$groupid;
			$this->view->schoolid=$schoolid;
			$this->view->orderid=$orderid;

			$sql="SELECT groupname FROM `groups` where groupid=".$groupid;
			$groupname=$this->_dbAdapter ->fetchOne ( $sql );
			$this->view->groupname=$groupname;
			
			$sql="SELECT schoolname,province,city,address FROM `school` where schoolid=".$schoolid;
			$school=$this->_dbAdapter ->fetchRow ( $sql );
			$this->view->school=$school;
			//print_r($school);exit;
			$sql="select classid from class where isdel=0 and schoolid=".$schoolid;
			$classid=$this->_dbAdapter->fetchCol($sql);
			$this->view->classnum=count($classid);
			$student=0;$teachernum=0;$contact=0;
		if($classid){
			$sql="select count(*) from teacher_class where inactive=0 and classid in (".implode(',',$classid).") ";
			$teachernum=$this->_dbAdapter->fetchOne($sql);
			
			$sql="select count(distinct studentid) from group_student_class_parents_v where inactive=0 and schoolid =".$schoolid;
			$student=$this->_dbAdapter->fetchOne($sql);
			
		}
			$sql="select * from school_contact where schoolid =".$schoolid;
			$contact=$this->_dbAdapter->fetchAll($sql);
			
			$sql="select * from service_school_order where productid=1 and school_orderid=".$orderid." and schoolid =".$schoolid;

			$service=$this->_dbAdapter->fetchRow($sql);
			//print_r($service);exit;
/*			if($service){
				foreach($service as $key=>$value){
					if(time()<$value['end_time'] && time()>$value['start_time']){
						$starttime=date('Y-m-d',$value['start_time']);
						$endtime=date('Y-m-d',$value['end_time']);
						$this->view->starttime=$starttime;
						$this->view->endtime=$endtime;
						$this->view->studentnum=$value['studentnum'];
					}
				}
			}
*/			
			$this->view->service=$service;
			$this->view->student=$student;
			$this->view->teachernum=$teachernum;
			$this->view->contact=$contact;
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	

	 public function editviewAction() {
		try {  	
		    $groupid = ( int ) $this->_request->getParam ( 'gid' );
		    $schoolid = $this->_request->getParam ( 'sid' ); 
			//print_r($groupid);print_r($schoolid);exit;
			$this->view->groupid=$groupid;
			$this->view->schoolid=$schoolid;
			$sql="SELECT groupname FROM `groups` where groupid=".$groupid;
			$groupname=$this->_dbAdapter ->fetchOne ( $sql );
			$this->view->groupname=$groupname;
			
			$sql="SELECT schoolname,province,city,address FROM `school` where schoolid=".$schoolid;
			$school=$this->_dbAdapter ->fetchRow ( $sql );
			$this->view->school=$school;
			//print_r($school);exit;
			$sql="select classid from class where isdel=0 and schoolid=".$schoolid;
			$classid=$this->_dbAdapter->fetchCol($sql);
			$this->view->classnum=count($classid);
			if($classid){
			$sql="select count(*) from teacher_class where inactive=0 and classid in (".implode(',',$classid).") ";
			$teachernum=$this->_dbAdapter->fetchOne($sql);
			$this->view->teachernum=$teachernum;
			
			$sql="select count(distinct studentid) from group_student_class_parents_v where inactive=0 and schoolid =".$schoolid;
			$student=$this->_dbAdapter->fetchOne($sql);
			$this->view->student=$student;
			}
			$sql="select * from school_contact where schoolid =".$schoolid;
			$contact=$this->_dbAdapter->fetchAll($sql);
			$this->view->contact=$contact;
			
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
      public function opeditviewAction() {
		try {
				if(! $this->isPost ()){
					throw new Exception ( '参数错误！' );
				}
				$p=$this->_request->getPost();
				//print_r($p);  exit;
				if(!intval($p['schoolid'])){
					throw new Exception ( '参数错误' );
				}				
	
				if(empty($p['province']) ||  empty($p['city']))
					throw new Exception ( '请选择所在城市' );
					
				foreach($p as $key =>$value){
					$p[$key]=addslashes(trim($value));
				}

			 	$res=$this->_dbAdapter->update ('school',array('address'=>$p['address'],'province'=>$p['province'],'city'=>$p['city'],'addtime'=>time()),'schoolid='.$p['schoolid']); 
				
				foreach($p['name'] as $key=>$value){
					$isok=0;
					$data=array(
						'contact_name'=>$value,
						'telephone'=>$p['telephone'][$key],
						'mobile'=>$p['mobile'][$key],
						'mail'=>$p['mail'][$key],
					);
					$res=$this->_dbAdapter->update ('school_contact',$data,'id='.$p['id'][$key]); 
					if($res)
						$isok=1;
				}
			 
			if ($res || $isok) {
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/detailed/sid/'.$p['schoolid'].'/gid/'.$p['groupid'] , 'tip', 3 );
			} else {
				//throw new Exception ( '操作失败,或已经删除，请先刷新查看！' );//操作失败,或已经删除，请先刷新查看！
				$this->feedback ( 'success', 'success', '/'.$this->controller.'/detailed/sid/'.$p['schoolid'].'/gid/'.$p['groupid'] , 'tip', 3 );
			}
	  
		} catch ( Exception $e ) {
					$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
				}
		}

	 public function addviewAction() {
		try { 
			$sql="SELECT groupid,groupname FROM `groups` where 1 ".$where;
			$group=$this->_dbAdapter ->fetchPairs ( $sql );
			$this->view->group=$group;
		} catch ( Exception $e ) {
			 $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
		}
	}
	
      public function opaddviewAction() {
		try {
			if (! $this->isPost ()) {
				throw new Exception ( '参数错误' );
			}
			$p =$this->_request->getPost ();
			//print_r($p);exit;

			if(empty($p['schoolname']))
				throw new Exception ( '学校名称不能为空' );
			if(empty($p['province']) ||  empty($p['city']))
				throw new Exception ( '请选择学校所在地区' );
			if(empty($p['address']))
				throw new Exception ( '请填写详细地址' );
			if(empty($p['groupid']))
				throw new Exception ( '请选择学校所属机构' );
			if(empty($p['admin_username']))
				throw new Exception ( '请填写用户名' );
			
			
			$sql = "select * from school where schoolname='".$p['schoolname']."' and groupid=".$p['groupid'];
			$result = $this->_dbAdapter->fetchRow ( $sql );
			if($result)
				throw new Exception ( '该学校已存在' );
				
			$sql="select cityid from weather_city where cityname='".$p['city']."'";//查询城市id
		
			$cityid = $this->_dbAdapter->fetchOne ( $sql );
			
			$sql = "select * from admin where adminname='".$p['admin_username']."' ";
			$result = $this->_dbAdapter->fetchRow ( $sql );
			if($result)
				throw new Exception ( '该管理员已经存在' );//该管理员已经存在！
			
			
			//用户基本信息
			$data=array(
				'cityid'=>$cityid,
				'schoolname'=>$p['schoolname'],
				'province'=>$p['province'],
				'city'=>$p['city'],
				'addtime' => time(),
				'address'=>$p['address'],
				'isofficial'=>1,
				'groupid'=>$p['groupid'],
			);
			$result = $this->_dbAdapter->insert ( 'school', $data );
			$sid = $this->_dbAdapter->lastInsertId ();//刚增加的自增id

			if ($sid) {
				$schoolid=$sid;
				$groupid=$p['groupid'];
/*				$sql="INSERT INTO `school_settings` VALUES 
				('$schoolid', 'attendance_type', '{\"1\":\"\\\u6b63\\\u5e38\",\"2\":\"\\\u4e8b\\\u5047\",\"3\":\"\\\u75c5\\\u5047\",\"4\":\"\\\u7f3a\\\u52e4\"}', '考勤类型JSON存储', '$groupid'),
				('$schoolid', 'course_week_limit', '5', '5或7，7为含周六、日', '$groupid'), 
				('$schoolid', 'teacher_duty','{\"1\":\"\\\u73ed\\\u4e3b\\\u4efb\",\"2\":\"\\\u8bed\\\u6587\\\u8001\\\u5e08\",\"3\":\"\\\u751f\\\u6d3b\\\u8001\\\u5e08\",\"4\":\"\\\u82f1\\\u6587\\\u8001\\\u5e08\",\"5\":\"\\\u73ed\\\u7ea7\\\u52a9\\\u7406\",\"6\":\"\\\u7f8e\\\u672f\\\u8001\\\u5e08\",\"7\":\"\\\u52a9\\\u6559\",\"8\":\"\\\u4fdd\\\u80b2\\\u5458\"}', '老师职务JSON存储',  '$groupid')";
				$this->_dbAdapter->query($sql);
*/			
			
/*				//('noticelimit', '15', '0', '0', '每次返回通知的数量', '$groupid', '$sid'),
				//('articlelimit', '15', '0', '0', '每次返回班级分享的数量', '$groupid', '$sid'), 
				('pull_rate_student', '3600', '0', '0', '推送通知的频率 家长端', '$groupid', '$schoolid'), 
				('pull_rate_teacher', '3600', '0', '0', '推送通知的频率 教师端', '$groupid', '$schoolid'), 
				('report_emails', 'chenjia@mac-top.com.cn', '0', '0', '收件人', '$groupid', '$schoolid'), 
				('logo', '', '0', '0', '学校logo设置', '$groupid', '$schoolid'), 
				('article_admin_check', '0', '0', '0', '班级日志需要管理员审核才能发布给家长', '$groupid', '$schoolid'), 
				('attendance_type', '{\"1\":\"\\\u6b63\\\u5e38\",\"2\":\"\\\u4e8b\\\u5047\",\"3\":\"\\\u75c5\\\u5047\",\"4\":\"\\\u7f3a\\\u52e4\"}', '0', '1', '考勤类型JSON存储', '$groupid', '$schoolid'), 
				('product_name', '云中校车', '0', '0', '产品名称', '$groupid', '$schoolid'), 
				('course_week_limit', '5', '0', '0', '5或7，7为含周六、日', '$groupid', '$schoolid'), 
				*/
							
				$sql="INSERT INTO `settings` VALUES 
				('weather_img_path', 'http://img.yunxiaoche.com/weather/', '1', '0', '天气的路径地址', '$groupid', '$schoolid'), 
				('school_type', '{\"1\":\"\\\u6167\\\u667a\",\"2\":\"\\\u6167\\\u4e50\",\"3\":\"\\\u53cc\\\u8bed\",\"4\":\"\\\u56fd\\\u9645\"}', '0', '1', '学校类型JSON存储', '$groupid', '$schoolid'), 
				('can_menu', '1', '1', '0', '允许食谱模块的使用', '$groupid', '$schoolid'), 
				('can_comment', '1', '1', '0', '允许日志评论功能的使用', '$groupid', '$schoolid'), 
				('can_comment_action', '1', '1', '0', '允许日志赞功能的使用', '$groupid', '$schoolid'), 
('photo_tag', '[[\"\\\u8499\\\u6c0f\\\u611f\\\u5b98\\\u533a\",\"\\\u8499\\\u6c0f\\\u6570\\\u5b66\\\u533a\",\"\\\u8499\\\u6c0f\\\u65e5\\\u5e38\\\u751f\\\u6d3b\\\u533a\",\"\\\u8499\\\u6c0f\\\u79d1\\\u5b66\\\u6587\\\u5316\\\u533a\",\"\\\u8499\\\u6c0f\\\u8bed\\\u8a00\\\u533a\",\"\\\u82f1\\\u6587\\\u96c6\\\u4f53\\\u8bfe\",\"\\\u4e2d\\\u6587\\\u96c6\\\u4f53\\\u8bfe\",\"\\\u6237\\\u5916\\\u6d3b\\\u52a8\",\"\\\u5c31\\\u9910\\\u65f6\\\u95f4\",\"\\\u7f8e\\\u5de5\\\u533a\",\"\\\u5efa\\\u6784\\\u533a\"],[\"Sensorial\",\"Mathematics\",\"Practical Life\",\"Culture Subjects\",\"Language\",\"English Circles\",\"Chinese Circles\",\"Outdoor Activities\",\"Meal Time\",\"Art\",\"Construction\"]]', '1', '1', '照片类型分类 json', '$groupid', '$schoolid'),
				
				('company', '云中校车', '0', '0', '公司名称', '$groupid', '$schoolid'), 
				('teacher_duty', '{\"1\":\"\\\u4e3b\\\u8bb2\\\u8001\\\u5e08\",\"2\":\"\\\u4e2d\\\u6587\\\u8001\\\u5e08\",\"3\":\"\\\u82f1\\\u6587\\\u8001\\\u5e08\",\"4\":\"\\\\u73ed\\\u7ea7\\\u52a9\\\u7406\"}', '0', '1', '老师职务JSON存储', '$groupid', '$schoolid'), 

('calendar_title', '{\"1\":\"\\\u5143\\\u65e6\",\"2\":\"\\\u6625\\\u8282\",\"3\":\"\\\u6e05\\\u660e\\\u8282\",\"4\":\"\\\u52b3\\\u52a8\\\u8282\",\"5\":\"\\\u7aef\\\u5348\\\u8282\",\"6\":\"\\\u4e2d\\\u79cb\\\u8282\",\"7\":\"\\\u6559\\\u5e08\\\u8282\",\"8\":\"\\\u56fd\\\u5e86\\\u8282\",\"9\":\"\\\u5723\\\u8bde\\\u8282\",\"10\":\"\\\u6691\\\u5047\",\"11\":\"\\\u5bd2\\\u5047\"}', '0', '1', '学生假期说明', '$groupid', '$schoolid'), 
				('menu_type', '{\"1\":\"\\\u65e9\\\u9910\",\"2\":\"\\\u65e9\\\u70b9\",\"3\":\"\\\u5348\\\u9910\",\"4\":\"\\\u5348\\\u70b9\",\"5\":\"\\\u665a\\\u9910\"}', '0', '1', '食谱类型设置', '$groupid', '$schoolid'), 
				('schedule_type', 'day', '1', '0', '课程表类型week 或day', '$groupid', '$schoolid'), 
				('prodevtime', 'yes', '0', '0', '设置教师专业培训日', '$groupid', '$schoolid')";

				$res3=$this->_dbAdapter->query($sql);
			
				//自动增加报告模板和照片分类标签
			
/**********111111111111111************/	
$template=array();	
$template=array('name' => '学生日报','type' => 1,'schoolid' => $schoolid,'groupid' => $groupid);			
$result = $this->_centraldbAdapter->insert ( 'report_templet', $template );
$id = $this->_centraldbAdapter->lastInsertId ();//刚增加的自增id

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '喝水多少','option2' => '较少','option1' => '正常','option3' => '过多');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '早餐情况','option2' => '较少','option1' => '正常','option3' => '过多');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '加餐吃了多少','option2' => '较少','option1' => '正常','option3' => '过多');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '午餐情况','option2' => '较少','option1' => '正常','option3' => '过多');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '午点水果吃了多少','option2' => '较少','option1' => '正常','option3' => '过多');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '晚餐情况','option2' => '较少','option1' => '正常','option3' => '过多');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '午睡情况','option2' => '无','option1' => '正常');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '今日排便统计','option2' => '无','option1' => '有');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content);

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '今日情绪状况','option2' => '低落','option1' => '一般','option3' => '积极');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '学习兴趣点');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '老师评价');			
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

/********22222222222222222**************/	
$template=array();	
$template=array('name' => '班级周计划','type' => 2,'schoolid' => $schoolid,'groupid' => $groupid);			
$result = $this->_centraldbAdapter->insert ( 'report_templet', $template );
$id = $this->_centraldbAdapter->lastInsertId ();//刚增加的自增id

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '本周教学重点');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '本周生活常规重点');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '本周户外活动安排');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '本周特殊活动及特色教学');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '本周任务以及家长需配合工作');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

/*********333333333*************/	
$template=array();	
$template=array('name' => '学生月报','type' => 3,'schoolid' => $schoolid,'groupid' => $groupid);			
$result = $this->_centraldbAdapter->insert ( 'report_templet', $template );
$id = $this->_centraldbAdapter->lastInsertId ();//刚增加的自增id

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '学习状态Learning state','option2' => '低落','option1' => '正常','option3' => '积极');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '学习模式Learning style','option2' => '依赖','option1' => '偏独立','option3' => '独立');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '与人相处Social behavior','option2' => '自我','option1' => '偏友好','option3' => '友好');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '日常生活Daily living activities','option2' => '需加强','option1' => '正常','option3' => '优秀');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '感官学习Sensorial','option2' => '需加强','option1' => '正常','option3' => '优秀');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '语言学习 Language arts','option2' => '需加强','option1' => '正常','option3' => '优秀');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '手工绘画 Handwork and painting','option2' => '需加强','option1' => '正常','option3' => '优秀');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '运动锻炼 Exercise training','option2' => '需加强','option1' => '正常','option3' => '优秀');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content);

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '优点&进步点 The strengths');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '成长建议 Key issues of development');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

/*******************444444444444444444***************/
$template=array();	
$template=array('name' => '学生体检报告','type' => 4,'schoolid' => $schoolid,'groupid' => $groupid);			
$result = $this->_centraldbAdapter->insert ( 'report_templet', $template );
$id = $this->_centraldbAdapter->lastInsertId ();//刚增加的自增id

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '身高');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '体重');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '听力','option1' => '正常','option2' => '异常');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '视力','option1' => '正常','option2' => '需矫治');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 1,'title' => '牙齿','option1' => '正常','option2' => '龋齿');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );

$content=array();
$content=array('reportid' => $id,'type' => 2,'title' => '体检结论');
$result = $this->_centraldbAdapter->insert ( 'report_templet_content', $content );


/*****************照片标签*********************/
/*****************1*********************/

$tag=array();
$tag=array('tagname' => '蒙氏感官区','tagname_en' => 'Sensorial','tagnamedesc' => '培养幼儿视、听、嗅、味、触五种感官能力的敏锐程度，使幼儿的每一项感觉都是灵敏全面发展。为学习数学及其他领域做准备，增强对于社会及知识的感知，培养初步的序列、分列概念和抽象思维能力。','tagnamedesc_en' => 'Cultivate kids sharp degree of the five senses ability--see, hear, smell, and touch, make every sense to all-round development.In preparation for the study mathematics and other fields, enhancement the awareness of social and intellectual, and cultivate the ability of sequence, respectively concepts and abstract thinking.','schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/****************2*********************/

$tag=array();
$tag=array('tagname' => '蒙氏数学区','tagname_en' => 'Mathematics','tagnamedesc' => '培养数的概念，理解加减乘除运算的关系；会做10000以内的加减乘除，会做简单的测量工作；培养逻辑思维能力和创造能力。','tagnamedesc_en' => 'Understand the concepts of number and the relationship between addition, subtraction, multiplication, and division. Have the ability to do the addition, subtraction, multiplication, and division within 1000, and simple measurements. Cultivate the ability of logical thinking and creative.','schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/*****************3********************/
$tag=array();
$tag=array('tagname' => '蒙氏日常生活区','tagname_en' => 'Practical Life','tagnamedesc' => '培养幼儿的大肌肉和小肌肉的组织能力，增强对自己身体的控制能力，从而把孩子从自然人向社会人过渡，脱离对父母的过度依赖。学习生活自理能力，学会帮助别人，照顾环境，培养对家庭、社会的责任感。形成良好的社会交往行为，为将来进入正式的社会生活做准备。','tagnamedesc_en' => "Cultivate kids\' ability of muscle tissue, enhance the capacity of control body, thus can help your kid transition from natural person to social person, out of excessive dependence on parents. Study self-care ability, to take care of environment, the responsibility of family and society. Form good social interaction, preparing to enter a formal social life in the future.",'schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/*****************4*********************/
$tag=array();
$tag=array('tagname' => '蒙氏科学文化区','tagname_en' => 'Culture Subjects','tagnamedesc' => '增长幼儿见识，开阔眼界，培养幼儿热爱科学的兴趣及探索求知的精神，使幼儿从小就具有创造性思维和实践能力，了解基本的动植物学、地理学、天文学、历史学知识。','tagnamedesc_en' => "Increase kids' knowledge, widen horizon, and cultivate kids\' interest in science and exploration spirit, to make kids own the ability of creative thinking and practical. Understand the basic knowledge of animal, plant science, geography, astronomy, and history.",'schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/*****************5*********************/
$tag=array();
$tag=array('tagname' => '蒙氏语言区','tagname_en' => 'Language','tagnamedesc' => '培养幼儿听说读写的能力，帮助幼儿更好的书写和阅读，使幼儿在童年时期就形成良好的阅读与书写习惯，培养喜爱阅读的兴趣、增强口语表达能力、强化听力与阅读能力，提高书写能力。','tagnamedesc_en' => "Cultivate kids\' ability of listen, speak, read and write, to help kids writing and reading better, and form good habits. Cultivate reading interest, strengthen the ability of oral English, listening , reading, and writing.",'schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/****************6********************/
$tag=array();
$tag=array('tagname' => '户外活动','tagname_en' => 'Outdoor Activities','tagnamedesc' => '课堂间隙户外锻炼，校园外户外活动等，可以使孩子拥有健康强壮的体质，更有阳光、开朗活泼的个性。','tagnamedesc_en' => 'Outdoor exercise and activities, etc. To make kids have a strong and healthy physique, and own optimistic, cheerful and lively personality.','schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/*****************7*********************/
$tag=array();
$tag=array('tagname' => '美工区','tagname_en' => 'Art','tagnamedesc' => '孩子进行绘画、手工制作等艺术学习，挖掘孩子的潜力，培养其自发学习的能力。','tagnamedesc_en' => "Learning painting, handmade art, etc. To dig kids' potential, and cultivate their ability of learning spontaneously.",'schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );

/****************8********************/
$tag=array();
$tag=array('tagname' => '就餐时间','tagname_en' => 'Meal Time','tagnamedesc' => '早餐、早点、午餐、午点、晚餐等学校餐点的进行，健康的饮食，给孩子健康的营养。','tagnamedesc_en' => 'Breakfast, snake AM, lunch, snake PM, dinner etc. The healthy diet, give the kids a healthy nutrition.','schoolid' => $schoolid);
$result = $this->_dbAdapter->insert ( 'school_tag', $tag );
			
			
			
			
				//用户基本信息
				$data=array('adminname'=>trim($p['admin_username']),
				'password'=>md5('111111'),
				'schoolid'=>$schoolid,
				'createtime' => time(),
				'groupid'=>$p['groupid'],
				'password_code'=>'111111'
				);
				$result = $this->_dbAdapter->insert ( 'admin', $data );
				
				foreach($p['name'] as $key=>$value){
					if($value){
						$isok=0;
						$data=array(
							'schoolid'=>$schoolid,
							'contact_name'=>$value,
							'telephone'=>$p['telephone'][$key],
							'mobile'=>$p['mobile'][$key],
							'mail'=>$p['mail'][$key],
						);
						$res=$this->_dbAdapter->Insert ('school_contact',$data); 
						if($res)
							$isok=1;
					}
				}
				
				$this->feedback ( '成功', '注意', '/'.$this->controller.'/detailed/sid/'.$schoolid.'/gid/'.$p['groupid'], 'tip',0); 		 
			} else {
				   $this->feedback ( $e->getMessage (), 'note', 'javascript:window.history.back();', 'warning' );
			}
		} catch ( Exception $e ) {
					$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
				}
		}
	
	 //以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

